/**
 * Meituan.com Inc.
 * Copyright (c) 2010-2018 All Rights Reserved.
 */
package com.tuyou.cloud.common.controller;

import com.tuyou.cloud.common.port.CommonInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * <p></p>
 *
 * @author tuyou@meituan.com
 * @version v0.1 2018-12-16 9:26 PM tuyou Exp $$
 */
@Slf4j
@RestController
public class HelloController implements CommonInterface {

    @Autowired
    private DiscoveryClient client;

//    @ResponseStatus
    @RequestMapping("/hello")
    public String hello(HttpServletResponse response) {
        ServiceInstance instance = client.getLocalServiceInstance();
        String str = "/hello, service_id:" + instance.getServiceId() + ", host:" + instance.getHost() + ", port:" + instance.getPort();
        logger.info(str);

        //测试返回一个错误码，FeignClient能自动抛出HystrixRuntimeException异常
//        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return str;
    }

    @RequestMapping(value = "/param", method = RequestMethod.GET)
    public String param(@RequestParam("name") String name, @RequestParam("age") Integer age) throws InterruptedException {
        String result = "name:" + name + ",age:" + age;
        int i = new Random().nextInt(3000);
        Thread.sleep(i);
        return result;
    }
}